package 简单.二叉树的层平均值;

import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution1 {
    public List<Double> averageOfLevels(TreeNode root) {
        //如果根节点为空，那么数组返回值为空
        //if(root == null){
            //return null;
        //}
        //新建列表，用于存放结果
        ArrayList<Double> ans = new ArrayList<>();

        //新建队列用户广度优先探索
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            double sum = 0;
            for(int i = 0 ; i < size; i++){
                TreeNode tmp = queue.poll();
                sum += tmp.val;
                if(tmp.left != null){
                    queue.offer(root.left);
                }
                if(tmp.right != null){
                    queue.offer(root.right);
                }
            }
            ans.add(sum/size);
        }
        return ans;
    }
}
